<?php
/*
 * 顾客
 */
class CustomerController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	protected $class;
	
	public function init() {
		$this->_user = $this->checkLogin ();
		
		/* Initialize action controller here */
		$this->controller = $this->getRequest ()->getControllerName ();
		$this->action = $this->getRequest ()->getActionName ();
		
		$this->commonTemplates ();
		
		$this->_smarty->assign ( 'controller', $this->controller );
		$this->_smarty->assign ( 'action', $this->action );
		$this->_smarty->assign ( '_user', $this->_user );
		
		$this->_smarty->assign ( 'title', '顾客' );
		
		//get database
		$this->getDb ( 'osmdb' );
		
		//verify rights
	//	if (isset ( $this->_user->agentcode ))
	//		$this->_redirect ( '/' );
		
	}
	
	public function indexAction() {
		try {
		$this->_smarty->assign ( 'childtitle', '顾客信息' );
			$count = 40; //pre
    			$curpage = ( int ) $this->_request->getParam ( 'page' );
    			if (! $curpage)
    				$curpage = 1;
    			$offset = ($curpage - 1) * $count;
			
			$sql = 'select SQL_CALC_FOUND_ROWS a.*
			from `crm_customer` a 
			where a.isdisabled=0 and a.shopid='.$this->_user->shopid.'
			order by a.`customerid` desc '." limit $offset,$count";
			$result = $this->_dbAdapter->fetchAssoc ( $sql );
			//print_r($result);
			foreach($result as $key => $value ){
				$result[$key]['sexDisp']= $value['sex']?'女':'男';
				$result[$key]['lastservicetimeDisp']= $value['lastservicetime']?date('y-m-d',$value['lastservicetime']):'未开始';
			}
			
			//翻页控制
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' );
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
    			$this->_smarty->assign ( 'allnum', $param ['totalnum'] );
    
    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    			//$page->setvar ( array ('gid' => $gradeid,'typeid'=>$typeid) );
    
    			$this->_smarty->assign ( 'page', $page->getNumPage () );
    			$this->_smarty->assign ( 'result', $result );
                $this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
		//设置管理员

	
	//增加登陆用户
	public function addAction() {
		try {
			$this->_smarty->assign ( 'childtitle', '增加新顾客' );
			//获取店面信息
			$sql='select shopid, shop_name from crm_shops where `isdisabled`=0';
			$shopinfo = $this->_dbAdapter->fetchPairs ( $sql );
			$this->_smarty->assign ( 'shopinfo', $shopinfo );	


						
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
			//print_r($this->controller);
			//print_r($this->action);			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//处理增加公告
	public function opaddAction() {
		try {
			if (! $this->isPost ()) {
				throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			
			//验证
			if(empty($p['name']) || empty($p['mobile'])){
				throw new Exception ( '姓名及手机不能为空！' );
			}
			
			$sql='select mobile from crm_customer where isdisabled=0 and mobile='.$p['mobile'];
			$mobile=$this->_dbAdapter->fetchOne($sql);
			if($mobile){
					throw new Exception ( '该手机已是会员，请查询！' );
			}
			$p['shopid']=$this->_user->shopid;
			$p['addstaffid']=$this->_user->uid;
			$p['addtime']=time();
			$p['updatetime']=$p['addtime'];
			
			$result = $this->_dbAdapter->insert ( 'crm_customer', $p );
			if ($result) {
				$this->feedback ( '保存成功', '成功', '/'.$this->controller, 'tip', 0 );
			} else {
				throw new Exception ( '增加失败！' );
			}
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	

	//修改管理员
	public function editAction() {
		try {
			$this->_smarty->assign ( 'childtitle', '修改顾客基本信息' );
						
			$id = ( int ) $this->_request->getParam ( 'id' );
			if (! $id)
				throw new Exception ( '参数错误！' );
				
			$sql='select * from `crm_customer` where `customerid`=' . $id;
			$result = $this->_dbAdapter->fetchRow ( $sql );

			$this->_smarty->assign ( 'result', $result );
			
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//处理修改管理员
	public function opeditAction() {
		try {
			if (! $this->isPost ()) {
				throw new Exception ( '参数错误！' );
			}
		
			$p =$this->_request->getPost ();

			//验证
			if(empty($p['name']) || empty($p['mobile'])){
				throw new Exception ( '姓名及手机不能为空！' );
			}
			$where='';
			$where .=' and shopid='.$this->_user->shopid;
			$sql='select mobile from crm_customer where isdisabled=0 and mobile='.$p['mobile'] .' and customerid<>'.$p ['customerid'].$where;
			$mobile=$this->_dbAdapter->fetchOne($sql);
			if($mobile){
					throw new Exception ( '该手机已是会员，不能修改！' );
			}
			$p['addstaffid']=$this->_user->uid;
			$p['updatetime']=time();			
				//print_r( $p ['uid'] );
			$result = $this->_dbAdapter->update ( 'crm_customer', $p, '`customerid`=' . $p ['customerid'] );	

			if ($result) {
				$this->feedback ( '保存成功', '成功', '/'.$this->controller, 'tip', 0 );
			} else {
				throw new Exception ( '修改失败！' );
			}
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//冻结顾客
	public function deleteAction() {
		try {
			$id = ( int ) $this->_request->getParam ( 'id' );
			if (! $id)
				throw new Exception ( '参数错误！' );
				
			$updateArr=array();
			$updateArr['isdisabled']=1;	
			$updateArr['updatetime']=time();	
			$updateArr['addstaffid']=$this->_user->uid;	
			$del_result = $this->_dbAdapter->update( 'crm_customer',$updateArr, 'customerid=' . $id);
				if ($del_result) {
				   $this->feedback ( '操作成功,请刷新查看！', '成功', '/'.$this->controller, 'tip', 0 );
				   
				} else {
				   throw new Exception ( '操作失败或已经删除，请先刷新查看！' );
						}
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//查看顾客信息
	public function viewAction() {
		try {
			$this->_smarty->assign ( 'childtitle', '查看顾客基本信息' );
						
			$id = ( int ) $this->_request->getParam ( 'id' );
			if (! $id)
				throw new Exception ( '参数错误！' );
				
			$sql='select * from `crm_customer` where `customerid`=' . $id;
			$result = $this->_dbAdapter->fetchRow ( $sql );
			$result['sexDisp']=$result['sex']?'女':'男';
			$this->_smarty->assign ( 'result', $result );
			
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}	
	
	//reserve statement as below
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		
		throw new Exception ( 'Invalid method' );
		
		// all other methods throw an exception
		throw new Exception ( 'Invalid method "' . $method . '" called' );
	}
}

